<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>商品展示系统</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .navbar {
            background-color: #333;
            color: white;
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .navbar h1 {
            margin: 0;
            font-size: 24px;
        }

        .navbar a {
            color: white;
            text-decoration: none;
            margin-left: 20px;
            font-size: 16px;
        }

        .container {
            display: flex;
            flex: 1;
        }

        .sidebar {
            width: 200px;
            background-color: #f4f4f4;
            padding: 20px;
            border-right: 1px solid #ccc;
        }

        .sidebar h2 {
            margin-bottom: 15px;
            font-size: 18px;
        }

        .sidebar ul {
            list-style: none;
            padding-left: 0;
        }

        .sidebar ul li {
            margin-bottom: 10px;
        }

        .sidebar ul li a {
            text-decoration: none;
            color: #333;
            font-size: 14px;
        }

        .sidebar ul li a:hover {
            text-decoration: underline;
        }

        .main {
            flex: 1;
            padding: 20px;
        }

        .product-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .product-table th,
        .product-table td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }

        .product-table th {
            background-color: #f2f2f2;
        }

        .product-table tr:nth-child(even) {
            background-color: #fafafa;
        }

        #message {
            color: red;
            margin-top: 10px;
        }
    </style>
</head>
<body>

<!-- 顶部导航栏 -->
<div class="navbar">
    <h1>商品管理系统</h1>
    <div>
        <a href="#">首页</a>
        <a href="#">购物车</a>
    </div>
</div>

<!-- 页面布局容器 -->
<div class="container">

    <!-- 左侧菜单 -->
    <div class="sidebar">
        <h2>功能菜单</h2>
        <ul>
            <li><a href="#">商品管理</a></li>
            <li><a href="#">商家信息</a></li>
            <li><a href="#">订单管理</a></li>
            <li><a href="#">用户反馈</a></li>
            <li><a href="#">设置</a></li>
        </ul>
    </div>

    <!-- 主体内容 -->
    <div class="main">
        <h2>商品列表</h2>
        <a href="insertGood.html">添加新商品</a>
        <div id="message"></div>
        <table class="product-table">
            <thead>
            <tr>
                <th>商品名称</th>
                <th>价格（元）</th>
                <th>库存数量</th>
                <th>上架时间</th>
            </tr>
            </thead>
            <tbody id="productList">
            <tr>
                <td colspan="4">正在加载商品数据...</td>
            </tr>
            </tbody>
        </table>
    </div>

</div>

<script>
    // 页面加载时请求商品列表
    axios.get('/goods/list')
        .then(function(response) {
            const products = response.data;
            const tbody = document.getElementById('productList');
            tbody.innerHTML = ''; // 清空旧数据

            if (products.length === 0) {
                tbody.innerHTML = '<tr><td colspan="4">暂无商品数据</td></tr>';
                return;
            }

            products.forEach(product => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${product.name}</td>
                    <td>${product.price}</td>
                    <td>${product.stock}</td>
                    <td>${product.date}</td>
                `;
                tbody.appendChild(row);
            });
        })
        .catch(function(error) {
            console.error('请求失败:', error);
            document.getElementById('message').innerText = '加载商品失败，请检查网络或稍后再试';
            document.getElementById('productList').innerHTML =
                `<tr><td colspan="4">无法加载商品数据</td></tr>`;
        });
</script>

</body>
</html>
